package zhoufu.sz.sanxiao_check.ui.adapter;

import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.ViewGroup;

import java.util.List;

import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.ui.adapter.items.FireSafeChildItem;
import zhoufu.sz.sanxiao_check.ui.adapter.items.ShowPicsItem;
import zhoufu.sz.sanxiao_check.ui.fragment.FireSafetyFragment;


/**
 * Created by hyc on 16-7-8.
 * 项目 ： SanXiao_Check
 * 开发 ： hyc
 * 时间 ： 16-7-8 上午10:28
 * 功能 ：
 */

public class FireSubItemAdapter extends FlexibleDBAdapter<Object, ShowPicsItem> {


    private final Object _fragment;
    private final FireSafeChildItem _fireSubItem;

    public FireSubItemAdapter(FireSafeChildItem fireSubItem, @NonNull List<AbstractFlexibleItem> items, Object editWithSearchFragment) {
        super(items, editWithSearchFragment);
        _fragment = editWithSearchFragment;
        /**
         * 如果items存在多个时
         */
        _fireSubItem = fireSubItem;
        /**
         * 当items只有一个
         */
//        bonusItem(items);
    }

    private void bonusItem(@NonNull List<AbstractFlexibleItem> items) {
        if (items.size() == 1) {
            ShowPicsItem item = (ShowPicsItem) items.get(0);
            if (item.getDB() == null || TextUtils.isEmpty(item.getDB().getImg())) {
                return;
            }
        }
        addItem(items.size(), null);
    }

    /**
     * 获取持有this 的Item
     *
     * @return
     */
    public FireSafeChildItem getFireSubItem() {
        return _fireSubItem;
    }

    @Override
    protected ShowPicsItem childItem(ViewGroup parent, int viewType) {
        return new ShowPicsItem();
    }

    @Override
    public boolean addItem(@IntRange(from = 0) int position, @NonNull AbstractFlexibleItem item) {

        if (item == null) {
            item = new ShowPicsItem();
        }

        return super.addItem(position, item);
    }

    public Object getFragment() {
        return _fragment;
    }

    @Override
    public void removeItem(@IntRange(from = 0) int position) {
        /**
         * 如果当前的size只为1 时  只删除数据
         */
        if (getItems().size() > 1)
            super.removeItem(position);
        //删除 Item
        if (_fragment instanceof FireSafetyFragment) {
            FireSafetyFragment fragment = (FireSafetyFragment) _fragment;
            fragment.onDelImg(this, position);
        }

    }

    @Override
    public void updateDataSet(List<AbstractFlexibleItem> items) {
//        bonusItem(items);
        super.updateDataSet(items);
    }
}